<?php
/**
* @package YaForms
* @copyright Авторские права (C) 2009 www.yar-it.com. Все права защищены.
* @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
* YaForms! - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
* Для получения информации о используемых расширениях и замечаний об авторском праве, смотрите файл copyright.php.
*/

defined( '_VALID_MOS' ) or die( 'Restricted access' );

require( JPATH_BASE.'/administrator/components/com_yaforms/config.yaforms.php' );
require( JPATH_BASE.'/administrator/components/com_yaforms/lib/af.lib.core.php' );
mosCommonHTML::loadJquery();
//mosCommonHTML::loadOverlib();

afLoadLib ( 'loadassets' );
showErrRep();

require_once( $mainframe->getPath('front_html') );

$formid = mosGetParam( $_REQUEST, 'formid' );

$Itemid = mosGetParam( $_GET, 'Itemid' );
if($Itemid == '' )$Itemid = afGetItemid( $formid );
if($Itemid == '' )$Itemid = '99999';
      

switch($task) {
   
   default:
   
      if (isset($formid)){

         ShowFrontYaForms( $formid, $option );
         if ( '1' === $afcfg_showfrontfooter )echo afFooter(0);
         
      } else {

         if ( '1' === $afcfg_loadfrontcss )afLoadFECSS();
         ShowFrontRootForms( $option );
         if ( '1' === $afcfg_showfrontfooter )echo afFooter();
         
      }
      
   break;
   
}


function ShowFrontYaForms( $formid, $option ){

   global $database, $mainframe, $my;
   
   $now = _CURRENT_SERVER_TIME;
   $nullDate = $database->getNullDate();

   $form_query = "SELECT *"
   . "\n FROM #__yaforms"
   . "\n WHERE id='$formid'"
   . "\n AND ( publish_up = " . $database->Quote( $nullDate ) . " OR publish_up <= " . $database->Quote( $now ) . " )"
   . "\n AND ( publish_down = " . $database->Quote( $nullDate ) . " OR publish_down >= " . $database->Quote( $now ) . " )"
   . "\n AND published = 1"
   . "\n AND access <= " . (int) $my->gid
   ;
   $database->setQuery( $form_query );
   $rows = $database->loadObjectList();

   if(empty($rows)){
   
      mosNotAuth();
      return;
   
   } else {
   
      $row = $rows[0];
   
      $item_query = "SELECT *"
      . "\n FROM #__yaforms_items"
      . "\n WHERE form_id='$formid'"
      . "\n ORDER BY `item_ordering` ASC"
      ;
      $database->setQuery( $item_query );
      $items = $database->loadObjectList();
   
      $params = new mosParameters( $row->attribs, $mainframe->getPath( 'com_xml', 'com_yaforms' ), 'component' );
   
      HTML_beyaforms::ShowFrontYaForms( $row, $items, $params, $option );

   }
   
}


function ShowFrontRootForms( $option ){

   global $database, $mainframe, $my;

   $now = _CURRENT_SERVER_TIME;
   $nullDate = $database->getNullDate();

   $query = "SELECT id, titel, published, access"
   . "\n FROM #__yaforms"
   . "\n WHERE published = 1"
   . "\n AND ( publish_up = " . $database->Quote( $nullDate ) . " OR publish_up <= " . $database->Quote( $now ) . " )"
   . "\n AND ( publish_down = " . $database->Quote( $nullDate ) . " OR publish_down >= " . $database->Quote( $now ) . " )"
   . "\n AND access <= " . (int) $my->gid
   . "\n ORDER BY ordering ASC"
   ;
   $database->setQuery( $query );
   $rows = $database->loadObjectList();

   $queryc = "SELECT a.id"
   . "\n FROM #__components AS a"
   . "\n WHERE a.option = 'com_yaforms'"
   . "\n AND a.parent = '0'"
   ;
   $database->setQuery( $queryc );
   $pid = $database->loadResult();

   $prow = new mosComponent( $database );
   $prow->load( $pid );

   $params = new mosParameters( $prow->params, $mainframe->getPath( 'com_xml', $prow->option ), 'component' );
        
   HTML_beyaforms::ShowFrontRootForms( $rows, $params, $option );
   
}


function ShowFrontRecivedForms( $option ){

   global $database, $mainframe, $mosConfig_list_limit;

   $limit = intval( mosGetParam($_REQUEST, 'limit', $mosConfig_list_limit) );
   $limitstart = intval( mosGetParam($_REQUEST, 'limitstart', 0) );

   $viewform = mosGetParam( $_GET, 'viewform', '' );

   $total_query = "SELECT count(a.id)"
   . "\n FROM #__yaforms_inbox AS a"
   . "\n ORDER BY id DESC"
   ;
   $database->setQuery( $total_query );
   $total = $database->loadResult();

   require_once( JPATH_BASE.'/administrator/components/com_yaforms/lib/af.lib.pagenav.php' );
   $pageNav = new mosPageNav( $total, $limitstart, $limit );

   $getonlyform = '';
   if($viewform != '')$getonlyform = "WHERE a.form_id = $viewform";

   $query = "SELECT *"
   . "\n FROM #__yaforms_inbox AS a $getonlyform"
   . "\n ORDER BY id DESC"
   ;
   $database->setQuery( $query, $pageNav->limitstart, $pageNav->limit );
   $rows = $database->loadObjectList();
        
   HTML_beyaforms::ShowFrontRecivedForms( $rows, $pageNav, $option );
   
}


function ShowFrontViewRecivedForms( $option ){

   global $database;

   $id = mosGetParam( $_GET, 'id' );

   $query = "SELECT *"
   . "\n FROM #__yaforms_inbox"
   . "\n WHERE id = $id"
   . "\n ORDER BY id DESC"
   ;
   $database->setQuery( $query );
   $rows = $database->loadObjectList();
   $row = $rows[0];
   
   HTML_beyaforms::ShowFrontViewRecivedForms( $row, $option );

}


function ShowFrontTableRecivedForms( $option ){

   global $database, $mainframe, $mosConfig_list_limit;

   $limit = intval( mosGetParam($_REQUEST, 'limit', $mosConfig_list_limit) );
   $limitstart = intval( mosGetParam($_REQUEST, 'limitstart', 0) );

   $viewform = mosGetParam( $_GET, 'viewform', '' );
   
   $total_query = "SELECT count(a.id)"
   . "\n FROM #__yaforms_inbox AS a"
   . "\n ORDER BY id DESC"
   ;
   $database->setQuery( $total_query );
   $total = $database->loadResult();

   require_once( JPATH_BASE.'/administrator/components/com_yaforms/lib/af.lib.pagenav.php' );
   $pageNav = new mosPageNav( $total, $limitstart, $limit );

   $getonlyform = '';
   if($viewform != '')$getonlyform = "WHERE a.form_id = $viewform";
   
   $query = "SELECT *"
   . "\n FROM #__yaforms_inbox AS a $getonlyform"
   . "\n ORDER BY id DESC"
   ;
   $database->setQuery( $query, $pageNav->limitstart, $pageNav->limit );
   $rows = $database->loadObjectList();

   HTML_beyaforms::ShowFrontTableRecivedForms( $rows, $pageNav, $option );

}


?>